package com.i2bt.app.share.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.adapter.changeShareAdapter;
import com.i2bt.app.BaseActivity;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.share.ShareChangeShareActivity;
import com.i2bt.bean.ShareBean;

import android.widget.CheckBox;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class ChangeShareJsonHandle extends BaseJsonModel {
	ShareChangeShareActivity act;
	ShareBean bean;

	public ChangeShareJsonHandle(BaseActivity activity) {
		super(activity);
		// TODO Auto-generated constructor stub
		act = (ShareChangeShareActivity) activity;
	}

	@Override
	protected String logicToWrap() throws JSONException {
		// TODO Auto-generated method stub
		bean = (ShareBean) beanParam;
		jsonParam.put(AndroidRestContract.ACTION, bean.getAct());
		jsonParam.put(AndroidRestContract.TOKEN, UserConfig.token);
		if (bean.getAct() == AndroidRestContract.GET_DETAIL_SHARE) {
			jsonParam.put(AndroidRestContract.SHARE_FOLDERS, bean.getPath());
		} else if (bean.getAct() == AndroidRestContract.CHANGE_SHARE) {
			jsonParam.put(AndroidRestContract.SHARE_FOLDERS, bean.getPath());

			JSONArray addlist = new JSONArray();
			JSONArray deletelist = new JSONArray();
			String a[] = bean.getAddList();
			String d[] = bean.getDeleteList();
			for (int i = 0; i < a.length; i++) {
				addlist.put(a[i]);
			}
			for (int i = 0; i < d.length; i++) {
				deletelist.put(d[i]);
			}
			jsonParam.put(AndroidRestContract.ADD_DETAIL_SHARE, addlist);
			jsonParam.put(AndroidRestContract.DELETE_DETAIL_SHARE, deletelist);
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		// TODO Auto-generated method stub
		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				if (succ) {
					if (bean.getAct() == AndroidRestContract.SHOW_FRIEND) {
						getAllFriend(ja);
					} else if (bean.getAct() == AndroidRestContract.GET_DETAIL_SHARE) {
						gethasFriend(ja);
					} else if (bean.getAct() == AndroidRestContract.CHANGE_SHARE) {
						changeshare(ja);
					}
				} else {
					mes = ja.getString(AndroidRestContract.MESSAGE);
					this.act.toastHandler(succ + mes);
					this.act.finish();
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.act.toastHandler("网络连接失败");
		}
	}

	public void getAllFriend(JSONObject ja) {
		try {
			JSONArray allFriend = ja
					.getJSONArray(AndroidRestContract.FRIEND_LIST);
			int length = allFriend.length();

			this.act.check = new CheckBox[length];
			this.act.has = new boolean[length];
			this.act.perState = new boolean[length];
			for (int i = 0; i < length; i++) {
				JSONArray j = allFriend.getJSONArray(i);
				this.act.friendId.add(j.getString(0));
				this.act.friendList.add(j.getString(1));
				this.act.has[i] = false;
				this.act.perState[i] = false;
			}
			ShareBean nextBean = new ShareBean();
			try {
				String s = new String(this.act.path);
				nextBean.setPath(URLEncoder.encode(s, "UTF-8"));
				System.out.println("caonima" + nextBean.getPath());
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			nextBean.setAct(AndroidRestContract.GET_DETAIL_SHARE);
			new ChangeShareJsonHandle(this.act).postTo(
					AndroidRestContract.REST_GET_DETAIL_SHARE, nextBean);
		} catch (JSONException e) {
			e.printStackTrace();
			this.act.finish();
		}
	}

	public void gethasFriend(JSONObject ja) {
		try {
			JSONArray hasFriend = ja.getJSONArray("friends");
			int length = hasFriend.length();

			this.act.index = new int[length];
			for (int i = 0; i < length; i++) {
				this.act.hasShareFriend.add(hasFriend.getString(i));
				this.act.index[i] = this.act.friendList
						.indexOf(this.act.hasShareFriend.get(i));
				this.act.has[this.act.index[i]] = true;
				this.act.perState[this.act.index[i]] = true;
			}
			this.act.adapter = new changeShareAdapter(this.act);
			this.act.list.setAdapter(this.act.adapter);
		} catch (JSONException e) {
			e.printStackTrace();
			this.act.finish();
		}
	}

	public void changeshare(JSONObject ja) {
		String mes;
		try {
			mes = ja.getString(AndroidRestContract.MESSAGE);
			this.act.toastHandler(mes);
			this.act.finish();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
